package com.pine.app.module.security.core;

import com.pine.app.module.security.core.common.enums.ErrorType;
import com.pine.app.module.security.oauth.exception.AuthenticationException;

/**
 * @author xiaoyuan
 * @create 2020/3/9 13:31
 **/
public class SecurityContext {

    private static final ThreadLocal<Authentication> contextHolder = new ThreadLocal();


    public  void setContext(Authentication authentication){
        if(authentication==null){
            throw new AuthenticationException(ErrorType.SERVICE_ERROR,"authentication不能为空");
        }
        contextHolder.set(authentication);
    }

    public  Authentication getAuthentication(){
        Authentication authentication =  contextHolder.get();
//        if(authentication==null){
//            throw new AuthenticationException(ErrorType.SERVICE_ERROR,"authentication为空");
//        }
        return authentication;
    }
    public void removeAuthentication(){
        contextHolder.remove();
    }
}
